mod command_bar;
mod message_bar;
mod status;
mod view;

use std::io::Error;

pub use command_bar::CommandBar;
pub use message_bar::MessageBar;
pub use status::StatusBar;
pub use view::View;

use crate::prelude::{RowIdx, Size};

// 所有UI组件都必须实现 UI trait，不然无法渲染该组件
pub trait UI {
    fn set_needs_redraw(&mut self, value: bool);

    fn needs_redraw(&self) -> bool;

    fn set_size(&mut self, size: Size);

    fn resize(&mut self, size: Size) {
        self.set_size(size);
        self.set_needs_redraw(true);
    }

    fn draw(&mut self, origin_row: RowIdx) -> Result<(), Error>;

    fn render(&mut self, origin_row: RowIdx) {
        // 检查是否需要重绘
        // 如果需要，则在屏幕上绘制
        if self.needs_redraw() {
            if let Err(err) = self.draw(origin_row) {
                #[cfg(debug_assertions)]
                {
                    panic!("Clound not render component: {err:?}");
                }

                #[cfg(not(debug_assertions))]
                {
                    let _ = err;
                }
            } else {
                self.set_needs_redraw(false);
            }
        }
    }
}
